package playn.platformer.entities;

import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;

import playn.platformer.Entity;
import playn.platformer.Game;

import tiled.simple.core.Animation;
import tiled.simple.core.MapObject;
import tiled.simple.core.Renderer;

public class Sonic implements Entity , Renderer {
	private Body body;
	private Game game;
	
	private Animation dream_Animation;

	private Animation runLeft_Animation;
	private Animation jumpLeft_Animation;
	private Animation standLeft_Animation;

	private Animation runRight_Animation;
	private Animation jumpRight_Animation;
	private Animation standRight_Animation;
	
	private static final int SLEEP_AFTER=15;
	
	public Sonic(Game game,MapObject mapObject) {
		this.game=game;
		
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(20/Game.PIXEL_PER_METER, 26/Game.PIXEL_PER_METER);

	
		FixtureDef fixtureDef = new FixtureDef();
		fixtureDef.shape = shape;
		fixtureDef.restitution = 0.0f;
		fixtureDef.friction=0.1f;
		
		
		BodyDef bodyDef = new BodyDef();
		bodyDef.type = BodyType.DYNAMIC;
		bodyDef.position.set(mapObject.getX()/Game.PIXEL_PER_METER,mapObject.getY()/Game.PIXEL_PER_METER);
		body = game.world.createBody(bodyDef);
		body.createFixture(fixtureDef);
		body.m_angularDamping=0.0f;
		body.m_linearDamping=0.1f;
		
        body.m_userData=mapObject.getType();
               
        dream_Animation=game.animations.getAnimation("dream");
        
        runLeft_Animation=game.animations.getAnimation("run_left");
        jumpLeft_Animation=game.animations.getAnimation("jump_left");
        standLeft_Animation=game.animations.getAnimation("stand_left");

        runRight_Animation=game.animations.getAnimation("run_right");
        jumpRight_Animation=game.animations.getAnimation("jump_right");
        standRight_Animation=game.animations.getAnimation("stand_right");
	}

	public void update(float delta) {
		if(game.controls.left){
			body.applyLinearImpulse(new Vec2(-delta/10,0), new Vec2(0,0));
			//body.applyForce(new Vec2(-30,0), new Vec2(0,0));
			direction=-1;
		} 
		if(game.controls.right){
			body.applyLinearImpulse(new Vec2(delta/10,0), new Vec2(0,0));
//			body.applyForce(new Vec2(30,0), new Vec2(0,0));
			direction=1;
		}
		
		limitHorizontalSpeed(8f);
		
		Vec2 v3=body.getLinearVelocity();
		if(Math.abs(v3.x)<0.05f){
			v3.x=0;
		} else {
			v3.x=v3.x*0.5f;
		}
		body.setLinearVelocity(v3);

		if(game.controls.up){
			if(game.contactListener.isHeroOnTheFloor()){
				body.applyLinearImpulse(new Vec2(0,-1.8f), new Vec2(0,0));
			}
		} else {
			Vec2 v=body.getLinearVelocity();
			if(v.y<0){
				v.y=v.y/1.5f;
			}
			body.setLinearVelocity(v);
		}
		
        i+=delta/60f;

		Vec2 position=body.getPosition();
		
		game.camera.setPosition((int)(position.x*Game.PIXEL_PER_METER),(int)(position.y*Game.PIXEL_PER_METER));
	}
	
	public void render() {
		
		Vec2 speed=body.getLinearVelocity();
		
		if(direction==1){  // right
			if(game.contactListener.isHeroOnTheFloor()){
				if(speed.x==0&&(!game.controls.right)){
					if(sleep>SLEEP_AFTER){
						dream_Animation.renderSprite(game.animationGraphics, this, (int)i/5);
					}else{
						standRight_Animation.renderSprite(game.animationGraphics, this, (int)i);
					}
					sleep+=0.1f;
				}else{
					runRight_Animation.renderSprite(game.animationGraphics, this, (int)i);
					sleep=0;
				}
			}else{
				jumpRight_Animation.renderSprite(game.animationGraphics, this, (int)i);
				sleep=0;
			}
		}else{             // left
			if(game.contactListener.isHeroOnTheFloor()){
				if(speed.x==0&&(!game.controls.left)){
					if(sleep>SLEEP_AFTER){
						dream_Animation.renderSprite(game.animationGraphics, this, (int)i/5);
					}else{
						standLeft_Animation.renderSprite(game.animationGraphics, this, (int)i);
					}
					sleep+=0.1f;
				}else{
					runLeft_Animation.renderSprite(game.animationGraphics, this, (int)i);
					sleep=0;
				}
			}else{
				jumpLeft_Animation.renderSprite(game.animationGraphics, this, (int)i);
				sleep=0;
			}
		}
    
	
	}
	
	private int direction=1; // +1 - right, -1 - left
	
	float i=0;
	float sleep=0;
	
	public void limitHorizontalSpeed(float limit){
		Vec2 v=body.getLinearVelocity();
		if (v.x<-limit){v.x=-limit;}
		if (v.x>limit){v.x=limit;}
		
		body.setLinearVelocity(v);
	}

	
	public void putImage(String source, int sx, int sy, int sw, int sh, int dx,int dy, int dw, int dh) {
		Vec2 pos=body.getPosition();
		int pos_x=(int) (pos.x*Game.PIXEL_PER_METER);
		int pos_y=(int) (pos.y*Game.PIXEL_PER_METER)+4;
		
		game.dynamicRenderer.putImage(source, sx, sy, sw, sh, pos_x +dx*2, pos_y +dy*2, dw*2, dh*2);
	}


}
